package com.htfyun.ft.fragment.factory;

import android.os.BatteryManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.htfyun.ft.R;
import com.htfyun.ft.common.BatteryInfo;
import com.htfyun.ft.utils.DebugLog;

import androidx.annotation.Nullable;

public class FragmentBattery extends FactoryBaseFragment {

    private TextView txtBattery;

    private BatteryInfo batteryInfo;

    private int previousBatteryStatus = -1;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_factory_battery;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        super.onInitView(savedInstanceState);

        txtBattery = (TextView) findViewById(R.id.txtBattery);

        batteryInfo = new BatteryInfo(getActivity());

        setBtnPassVisibility(View.GONE);
    }

    @Override
    protected void onVisible() {
        super.onVisible();

        batteryInfo.startMonitor();
        batteryInfo.setOnBatteryChangedListener(batteryChangedListener);

        previousBatteryStatus = -1;
    }

    @Override
    protected void onInvisible() {
        super.onInvisible();

        batteryInfo.setOnBatteryChangedListener(null);
        batteryInfo.stopMonitor();
    }


    private BatteryInfo.OnBatteryChangedListener batteryChangedListener =
            new BatteryInfo.OnBatteryChangedListener() {
                @Override
                public void onBatteryChangedListener(BatteryInfo info) {

                    txtBattery.setText(getString(R.string.test_battery_charge_status, batteryInfo.getBatteryStatusString()));

                    int status = batteryInfo.getBatteryStatus();

                    if (previousBatteryStatus > 0
                            && previousBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
                            && status != BatteryManager.BATTERY_STATUS_UNKNOWN
                            && previousBatteryStatus != status) {

                        delayAutoPass();
                    }

                    previousBatteryStatus = status;

                }
            };


}
